package com.totcy.atom.view.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.animation.DecelerateInterpolator;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.totcy.atom.CommonValues;
import com.totcy.atom.R;
import com.totcy.atom.adapter.GankImageListAdapter;
import com.totcy.atom.bean.GankTypeBean;
import com.totcy.atom.presenter.GankTypeListPresenter;
import com.totcy.atom.presenter.contract.GankTypeListContract;
import com.totcy.atom.utils.glide.GlideUtil;
import com.totcy.atom.view.activity.ImageListActivity;
import com.totcy.atom.view.activity.PictureBrowsingActivity;
import com.totcy.baselibrary.adapter.BaseLoadMoreAdapter;
import com.totcy.baselibrary.adapter.loadmore.OnLoadMoreListener;
import com.totcy.baselibrary.utils.SPUtil;
import com.totcy.baselibrary.utils.StatusBarUtil;
import com.totcy.baselibrary.widget.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.totcy.atom.view.fragment.HomeMineFragment.USER_IMG;

/**
 * description: 主页图片
 *
 * @author: Totcy
 * date: 2017/10/31 17:34
 * update: 2017/10/31
 */

public class HomeImageFragment extends BaseFragment<GankTypeListContract.View,GankTypeListPresenter> implements GankTypeListContract.View, OnLoadMoreListener, OnRefreshListener, BaseLoadMoreAdapter.OnItemClickListener, NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.recyclerView)
    RecyclerView rvImage;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerlayout;
    @BindView(R.id.navigationView)
    NavigationView navigationView;

    private static final String type = "福利";
    private int pageNum = 1;
    private static final int pageSize = 10;
    private List<GankTypeBean> list = new ArrayList<>();
    private GankImageListAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_image;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        //fragment自己适配 toolbar 增加title layout paddingTop
        StatusBarUtil.setPaddingSmart(getActivity(), toolbar);

        setActionBar();
        setDrawerToggle();
        navigationView.setNavigationItemSelectedListener(this);

        LinearLayoutManager lm = new LinearLayoutManager(getActivity());
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        rvImage.setLayoutManager(lm);
        adapter = new GankImageListAdapter(getActivity(), list);
        rvImage.setAdapter(adapter);

        adapter.setOnLoadMoreListener(rvImage, this);
        adapter.setOnItemClickListener(this);
        //设置回弹动画插值器 减速效果
        refresh.setReboundInterpolator(new DecelerateInterpolator());
        refresh.setOnRefreshListener(this);
        //自动刷新
        refresh.autoRefresh(200);

        showUserImg();
    }

    /**
     * 读取本地用户头像
     */
    private void showUserImg(){
        if (!TextUtils.isEmpty((String) SPUtil.get(getActivity(), USER_IMG, ""))) {
            GlideUtil.loadImage(getActivity(), SPUtil.get(getActivity(), USER_IMG, ""), (CircleImageView) navigationView.getHeaderView(0).findViewById(R.id.iv_userImg),false);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            showUserImg();
        }
    }

    /**
     * 设置ActionBar
     */
    private void setActionBar() {
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        /*显示Home图标*/
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    /**
     * 设置Drawerlayout的开关,并且和Home图标联动
     */
    private void setDrawerToggle() {
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(getActivity(), drawerlayout, toolbar, 0, 0);
        drawerlayout.addDrawerListener(toggle);
        //同步drawerlayout的状态
        toggle.syncState();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        pageNum = 1;
        getPresenter().getGankTypeData(type, pageSize, pageNum);
    }

    @Override
    public void onLoadMore() {
        pageNum++;
        getPresenter().getGankTypeData(type, pageSize, pageNum);
    }

    @Override
    public void onClickLoadMore() {
        getPresenter().getGankTypeData(type, pageSize, pageNum);
    }

    @Override
    public void onGankTypeData(List<GankTypeBean> images) {
        refresh.finishRefresh(true);

        if (pageNum == 1) {
            list.clear();
        }
        list.addAll(images);
        adapter.notifyDataSetChanged();
        adapter.setLoadMoreFinish(images.size() == pageSize);
    }

    @Override
    public void onDataFail() {
        adapter.setLoadFail();
        refresh.finishRefresh(false);
    }

    @Override
    public void onItemClick(int position) {
        ArrayList<String> urlList = new ArrayList<>();
        urlList.add(list.get(position).getUrl());
        Bundle bundle = new Bundle();
        bundle.putInt("position", 0);
        bundle.putStringArrayList("urlList", urlList);
        openActivity(PictureBrowsingActivity.class, bundle);
    }

    /**
     * Called when an item in the menu_main_bottom_navigation menu is selected.
     *
     * @param item The selected item
     * @return true to display the item as the selected item
     */
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Bundle bundle = new Bundle();
        switch (item.getItemId()) {
            case R.id.single_0:
                bundle.putString("typeName", getString(R.string.navigation_menu_0));
                bundle.putString("type", CommonValues.MAIN);
                bundle.putInt("typePosition", 0);
                break;
            case R.id.single_1:
                bundle.putString("typeName", getString(R.string.navigation_menu_1));
                bundle.putString("type", CommonValues.XINGGAN);
                bundle.putInt("typePosition", 6);
                break;
            case R.id.single_2:
                bundle.putString("typeName", getString(R.string.navigation_menu_2));
                bundle.putString("type", CommonValues.QINGCHUN);
                bundle.putInt("typePosition", 1);
                break;
            case R.id.single_3:
                bundle.putString("typeName", getString(R.string.navigation_menu_3));
                bundle.putString("type", CommonValues.XIAOHUA);
                bundle.putInt("typePosition", 2);
                break;
            case R.id.single_4:
                bundle.putString("typeName", getString(R.string.navigation_menu_4));
                bundle.putString("type", CommonValues.CHEMO);
                bundle.putInt("typePosition", 3);
                break;
            case R.id.single_5:
                bundle.putString("typeName", getString(R.string.navigation_menu_5));
                bundle.putString("type", CommonValues.QIPAO);
                bundle.putInt("typePosition", 4);
                break;
            case R.id.single_6:
                bundle.putString("typeName", getString(R.string.navigation_menu_6));
                bundle.putString("type", CommonValues.MINGXING);
                bundle.putInt("typePosition", 5);
                break;
            default:
                break;
        }
        openActivity(ImageListActivity.class, bundle);
        drawerlayout.closeDrawer(GravityCompat.START);
        return true;
    }
}
